//
//  ZWComposeToolBar.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/26.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit
enum ZWToolbarButtonType: Int {
    case Picture = 0 , Mention, Trend, Emoticon, Add
}

@available(iOS 9.0, *)
class ZWComposeToolBar: UIStackView {
    
    var toolbarClosure:((type:ZWToolbarButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        distribution = .FillEqually
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addChildButton("compose_toolbar_picture",type: .Picture)
        addChildButton("compose_mentionbutton_background",type: .Mention)
        addChildButton("compose_trendbutton_background",type: .Trend)
        addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        addChildButton("compose_add_background",type: .Add)
    }
    
    @objc private func clickButton(button:UIButton){
//       print(button.tag)
        toolbarClosure?(type:ZWToolbarButtonType(rawValue: button.tag)!)
    }
    
    private func addChildButton(imageNamed:String,type:ZWToolbarButtonType){
        let button = UIButton()
        button.addTarget(self, action: #selector(ZWComposeToolBar.clickButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
        //设置button的tag
        button.tag = type.rawValue
        // 设置不同状态的图片
        button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addArrangedSubview(button)
    }
}
